import requests
from bs4 import BeautifulSoup
url = "https://www.umei.cc/tupiandaquan/"
resp = requests.get(url)
# resp.encoding = "utf-8"  # 中文字符处理
print(resp.text)
all_page = BeautifulSoup(resp.text, "html.parser")
# tables = all_page.find('div', class_='taotu-main')  # 范围还是太大了
all_list = all_page.find('div', class_='taotu-main').find_all('a')  # 再次缩小了范围
# print(all_list)
for a in all_list:
    img_html = a.get('href')
    # print(img_html)
    child_resp = f"https://www.umei.cc{img_html}"
    # print(child_resp)
    child_resp_url = requests.get(child_resp)
    child_resp_url.encoding = "utf-8"
    # print(child_resp_url.text)
    child_page = BeautifulSoup(child_resp_url.text, "html.parser")
    img = child_page.find('div', class_='big-pic').find('img').get('src')
    # print('图片地址：', img)
    img_resp = requests.get(img)
    img_url = img_resp.content  # 以文本的方式获得图片
    img_name = img.split('/')[-1]  # 获取图片名称
    # print(img_url)
    # with open(img_name, mode='wb') as f:  # 得到的是在根目录
    #     f.write(img_url)
    with open(f"img/{img_name}", mode='wb') as f:  # 得到的是在img目录下
        f.write(img_url)
    print(f"下载完成：{img_name}")
    child_resp_url.close()
resp.close()